home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-10-12 | 17.7 KB | 656 lines |
- package symantec.itools.awt.util;
-
- import java.applet.*;
- import java.beans.PropertyVetoException;
- import java.beans.PropertyChangeListener;
- import java.beans.VetoableChangeListener;
- import java.beans.Beans;
-
-
- // Created and implemented by Levi Brown, Symantec Macintosh Internet Tools.
- // 04/11/97 LAB Checked it in
- // 04/17/97 LAB Changed a few names to comply with Java standards.
- // Added automatic initialization of the applet context in the constructor.
- // Changed the function comment style to comply with JavaDoc standards.
- // Took out some unnecessary code in setAppletContext(Applet a).
- // Reorganized the code, to group public and private methods separately.
- // Removed some unnecessary import statements.
- // Fixed a bug in setAppletContext(Applet a) that could cause a NullPointerException.
- // Added set/getAutoStart(boolean f) to allow the scrolling to occur without a direct call to start().
- // Changed the default state of execute to true, so scrolling would automatically occur.
- // 06/02/97 LAB Updated to Java 1.1
- // 09/09/98 MSH Changed class so that it no longer creates threads at design-time, #62541
-
- /**
- * Displays a scrolling message in the status window of a browser or
- * applet viewer.
- *
- * @version 1.1, June 2, 1997
- * @author Symantec
- */
- public class StatusScroller implements Runnable
- {
- /**
- * Constructs a StatusScroller.
- * By default, the message (set by the setString() method) will scroll
- * right-to-left, repeat, and will completely scroll off before scrolling
- * on again.
- * @see #setString
- */
- public StatusScroller()
- {
- isRightToLeft = true;
- isRepeat = true;
- isScrollClean = true;
- execute = true;
- loopFlag = false;
- statusString = null;
- delay = 100;
- if ( !Beans.isDesignTime() )
- thread = new Thread(this);
-
- //Initalize the context variable
- try
- {
- setAppletContext(symantec.itools.lang.Context.getApplet());
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
-
- if ( !Beans.isDesignTime() )
- {
- thread.start();
- }
-
- }
-
-
- //***** Public Methods *****//
-
- /**
- * Sets the string to be scrolled in the browser or applet viewer.
- *
- * @param s the message to scroll
- * @see #getString
- *
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setString(String s) throws PropertyVetoException
- {
- if (context == null) {
- //Initalize the context variable
- try
- {
- setAppletContext(symantec.itools.lang.Context.getApplet());
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
-
- if(!symantec.itools.util.GeneralUtils.objectsEqual(statusString, s))
- {
- String oldValue = statusString;
-
- vetos.fireVetoableChange("String", oldValue, s);
-
- statusString = s;
- index = 0;
-
- if(statusString == null || statusString.equals(""))
- {
- statusString = null;
- workingBuffer = null;
- wblength = 0;
- sslength = 0;
- if(execute)
- clear();
- }
- else
- {
- sslength = statusString.length();
- updateWorkingBuffer();
- }
-
- changes.firePropertyChange("String", oldValue, statusString);
- }
- }
-
- /**
- * Gets the string being scrolled.
- * @return the current scroll string
- * @see #setString
- */
- public String getString()
- {
- return statusString;
- }
-
- /**
- * Controls the direction the text will scroll.
- * @param b the direction to scroll the text;
- * true for right-to-left, false for left-to-right
- * @see #getRightToLeft
- *
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setRightToLeft(boolean b) throws PropertyVetoException
- {
- Boolean oldValue = new Boolean(isRightToLeft);
- Boolean newValue = new Boolean(b);
-
- vetos.fireVetoableChange("RightToLeft", oldValue, newValue);
-
- isRightToLeft = b;
-
- changes.firePropertyChange("RightToLeft", oldValue, newValue);
- }
-
- /**
- * Gets the direction the text will scroll.
- * @return true if the text will scroll right-to-left,
- * false if the text will scroll left-to-right
- * @see #setRightToLeft
- */
- public boolean isRightToLeft()
- {
- return isRightToLeft;
- }
-
- /**
- * @deprecated
- * @see #isRightToLeft
- */
- public boolean getRightToLeft()
- {
- return isRightToLeft();
- }
-
- /**
- * Controls whether text will scroll completely off before scrolling on
- * again.
- * @param b if true the text will scroll completely off before scrolling
- * on again, if false the text will scroll without any gap
- * @see #isScrollClean
- *
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setScrollClean(boolean b) throws PropertyVetoException
- {
- if(isScrollClean != b)
- {
- Boolean oldValue = new Boolean(isScrollClean);
- Boolean newValue = new Boolean(b);
-
- vetos.fireVetoableChange("ScrollClean", oldValue, newValue);
-
- isScrollClean = b;
- updateWorkingBuffer();
-
- changes.firePropertyChange("ScrollClean", oldValue, newValue);
- }
- }
-
- /**
- * Gets whether text will scroll completely off before scrolling on
- * again.
- * @return true if the text will scroll completely off before scrolling
- * on again, false if the text will scroll without any gap
- * @see #setScrollClean
- */
- public boolean isScrollClean()
- {
- return isScrollClean;
- }
-
- /**
- * @deprecated
- * @see #isScrollClean
- */
- public boolean getScrollClean()
- {
- return isScrollClean();
- }
-
- /**
- * Controls whether scrolling will automatically start when the applet is loaded.
- * @param f if true the text will start scrolling as soon as the applet
- * is loaded, if false the text will not scroll until start() is called
- * @see #isAutoStart
- * @see #start
- * @see #stop
- *
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setAutoStart(boolean f) throws PropertyVetoException
- {
- Boolean oldValue = new Boolean(isAutoStart());
- Boolean newValue = new Boolean(f);
-
- vetos.fireVetoableChange("AutoStart", oldValue, newValue);
-
- if(f)
- start();
- else
- stop();
-
- changes.firePropertyChange("AutoStart", oldValue, newValue);
- }
-
- /**
- * Gets whether scrolling will automatically start when the applet is loaded.
- * @return true if the text will start scrolling as soon as the applet is loaded,
- * false if the text will not scroll until start() is called
- * @see #setAutoStart
- * @see #start
- * @see #stop
- */
- public boolean isAutoStart()
- {
- return execute;
- }
-
- /**
- * @deprecated
- * @see #isAutoStart
- */
- public boolean getAutoStart()
- {
- return isAutoStart();
- }
-
- /**
- * Controls whether text will repeatedly scroll or just scroll once.
- * @param f if true the text will scroll over and over,
- * if false the text will scroll off and back on, then stop
- * @see #isRepeat
- *
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setRepeat(boolean f) throws PropertyVetoException
- {
- Boolean oldValue = new Boolean(isRepeat);
- Boolean newValue = new Boolean(f);
-
- vetos.fireVetoableChange("Repeat", oldValue, newValue);
-
- isRepeat = f;
-
- changes.firePropertyChange("Repeat", oldValue, newValue);
- }
-
- /**
- * Gets whether text will repeatedly scroll or just scroll once.
- * @return true if the text will scroll over and over,
- * false if the text will scroll off and back on, then stop
- * @see #setRepeat
- */
- public boolean isRepeat()
- {
- return isRepeat;
- }
-
- /**
- * @deprecated
- * @see #isRepeat
- */
- public boolean getRepeat()
- {
- return isRepeat();
- }
-
- /**
- * Sets the time between the display of each character in milliseconds.
- * The minimum delay is 30 milliseconds.
- * @param d the delay, in milliseconds
- * @see #getDelay
- *
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setDelay(int d) throws PropertyVetoException
- {
- int temp = 0;
-
- if(d < 30)
- temp = 30;
- else
- temp = d;
-
- Integer oldValue = new Integer(delay);
- Integer newValue = new Integer(temp);
-
- vetos.fireVetoableChange("Delay", oldValue, newValue);
-
- delay = temp;
-
- changes.firePropertyChange("Delay", oldValue, newValue);
- }
-
- /**
- * Gets the time between the display of each character in milliseconds.
- * @return the delay, in milliseconds
- * @see #setDelay
- */
- public int getDelay()
- {
- return delay;
- }
-
- /**
- * Sets the AppletContext that has the status area for scrolling text.
- * Note: this overrides the automatically set AppletContext.
- *
- * @param c the new AppletContext
- * @see #setAppletContext(java.applet.Applet)
- *
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setAppletContext(AppletContext c) throws PropertyVetoException
- {
- AppletContext oldValue = context;
-
- vetos.fireVetoableChange("AppletContext", oldValue, c);
-
- context = c;
-
- changes.firePropertyChange("AppletContext", oldValue, c);
- }
-
- /**
- * Sets the AppletContext that has the status area for scrolling text.
- * This version takes an Applet object and gets the needed AppletContext from that.
- * Note: this overrides the automatically set AppletContext.
- *
- * @param a the Applet with the AppletContext to use
- * @see #setAppletContext(java.applet.AppletContext)
- *
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setAppletContext(Applet a) throws PropertyVetoException
- {
- if(a == null)
- setAppletContext((AppletContext)null);
- else
- setAppletContext(a.getAppletContext());
- }
-
- /**
- * Starts the status text scrolling.
- * @see #stop
- * @see #run
- */
- public void start()
- {
- execute = true;
- if ( !Beans.isDesignTime() )
- thread.resume();
- }
-
- /**
- * Stops the status text scrolling.
- * @see #start
- */
- public void stop()
- {
- execute = false;
- }
-
- /**
- * Clears the status area.
- * Note: this does not clear the string to scroll,
- * it just clears the status area in the current AppletContext.
- */
- public void clear()
- {
- if(context != null)
- {
- //Display an empty string
- context.showStatus("");
- }
- }
-
- /**
- * The body of the StatusScroller Thread.
- * This method is called by the Java Virtual Machine in response to a
- * call to the start method of this object.
- */
- public void run()
- {
-
- String workingString = null;
- int loopIndicator;
-
- if(!execute && !Beans.isDesignTime() ) thread.suspend();
- try
- {
- while(true)
- {
- do
- {
- //loopIndicator = index;
- if ( !Beans.isDesignTime() )
- thread.sleep(delay);
-
- if (execute)
- {
- //Get the string to display
- workingString = scrollString();
- if (context != null && workingString != null)
- {
- //Display the string
- context.showStatus(workingString);
- }
- }
- }
- //Keep scrolling until an exit condition is met
- while (index != 0 || isRepeat);
-
- if ( !Beans.isDesignTime() )
- thread.suspend();
- }
- }
- catch (InterruptedException e)
- {
- }
- }
-
- /**
- * Adds a listener for all property change events.
- * @param listener the listener to add
- * @see #removePropertyChangeListener
- */
- public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
- {
- changes.addPropertyChangeListener(listener);
- }
-
- /**
- * Removes a listener for all property change events.
- * @param listener the listener to remove
- * @see #addPropertyChangeListener
- */
- public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
- {
- changes.removePropertyChangeListener(listener);
- }
-
- /**
- * Adds a listener for all vetoable property change events.
- * @param listener the listener to add
- * @see #removeVetoableChangeListener
- */
- public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
- {
- vetos.addVetoableChangeListener(listener);
- }
-
- /**
- * Removes a listener for all vetoable property change events.
- * @param listener the listener to remove
- * @see #addVetoableChangeListener
- */
- public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
- {
- vetos.removeVetoableChangeListener(listener);
- }
-
- //***** Private Methods *****//
-
- /**
- * Handles the manipulation of the string buffer to give the
- * desired scrolling effect.
- * @return the string to be displayed
- * @see #run
- * @see #setRightToLeft
- * @see #getRightToLeft
- */
- protected String scrollString()
- {
- //If the string is null
- if(workingBuffer == null)
- return null;
- //If there is only one character
- if(wblength < 2)
- return(workingBuffer.toString());
-
- char ch, temp[];
- //Allocate space for the rest of the characters
- temp = new char[wblength - 1];
-
- if(isRightToLeft)
- {
- //Store the first character
- ch = workingBuffer.charAt(0);
- //Store the rest of the characters
- workingBuffer.getChars(1, wblength, temp, 0);
- //Shift the rest of the characters to the left one index
- workingBuffer = new StringBuffer(new String(temp));
- //Append the first character
- workingBuffer = workingBuffer.append(ch);
- //Keep track of where the original first character is
- index++;
- if(index > wblength -1)
- index = 0;
- }
- else
- {
- //Store the last character
- ch = workingBuffer.charAt(wblength - 1);
- //Store the rest of the characters
- workingBuffer.getChars(0, wblength - 1, temp, 0);
- //Place the last character at the beginning
- workingBuffer = new StringBuffer("" + ch);
- //Append the rest of the characters
- workingBuffer = workingBuffer.append(temp);
- //Keep track of where the original first character is
- index--;
- if(index < 0)
- index = wblength -1;
- }
-
- //Return a window onto the modified string buffer to be displayed
- return((workingBuffer.toString()).substring(0, sslength));
- }
-
- /**
- * Returns a String with howBig number of spaces as the content
- * @param howBig the requested number of spaces
- * @return a String with the requested number of spaces
- */
- protected String makePadding(int howBig)
- {
- StringBuffer str = new StringBuffer(0);
-
- for(int i = 0; i < howBig; ++i)
- str.append(" ");
- return(str.toString());
- }
-
- /**
- * Updates the string buffer depending on the current settings.
- * @see #setScrollClean
- */
- protected void updateWorkingBuffer()
- {
- if(isScrollClean)
- {
- String temp = makePadding(sslength);
- workingBuffer = new StringBuffer(statusString + temp);
- }
- else
- workingBuffer = new StringBuffer(statusString);
-
- wblength = workingBuffer.length();
- }
-
- /**
- * If true, the text will scroll from the right to the left.
- * @see #setRightToLeft
- * @see #isRightToLeft
- */
- protected boolean isRightToLeft;
- /**
- * If true, the text will continue scrolling over and over.
- * @see #setRepeat
- * @see #isRepeat
- */
- protected boolean isRepeat;
- /**
- * If true, the text will scroll completely off before scrolling on again.
- * @see #setScrollClean
- * @see #isScrollClean
- */
- protected boolean isScrollClean;
- /**
- * The applet context that shows the status text.
- */
- protected AppletContext context;
- /**
- * The thread that handles scrolling the text.
- * @see #start
- * @see #stop
- */
- protected Thread thread;
- /**
- * The time between the display of each character in milliseconds.
- * @see #setDelay
- * @see #getDelay
- */
- protected int delay;
- /**
- * Working buffer length, in characters.
- */
- protected int wblength;
- /**
- * Status string length, in characters.
- */
- protected int sslength;
- /**
- * The zero-relative index of the original first character.
- */
- protected int index;
- /**
- * The string to scroll.
- */
- protected String statusString;
- /**
- * The string that gets scrolled.
- */
- protected StringBuffer workingBuffer;
-
- private boolean execute, loopFlag;
- private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
- private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
- }
-